﻿using ZYCommon;
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Xml;

namespace TextEditor
{
   

    public class ZYTextBorder
    {
        public Color backColor = DefaultBackColor;
        public Color bottomColor = DefaultColor;
        public ButtonBorderStyle bottomStyle = ButtonBorderStyle.Solid;
        public int bottomWidth = 0;
        public const string c_BackGround_Color = "background-color";
        public const string c_Border_Bottom_Color = "border-bottom-color";
        public const string c_Border_Bottom_Style = "border-bottom-style";
        public const string c_Border_Bottom_Width = "border-bottom-width";
        public const string c_Border_Color = "border-color";
        public const string c_Border_Left_Color = "border-left-color";
        public const string c_Border_Left_Style = "border-left-style";
        public const string c_Border_Left_Width = "border-left-width";
        public const string c_Border_Right_Color = "border-right-color";
        public const string c_Border_Right_Style = "border-right-style";
        public const string c_Border_Right_Width = "border-right-width";
        public const string c_Border_Style = "border-style";
        public const string c_Border_Top_Color = "border-top-color";
        public const string c_Border_Top_Style = "border-top-style";
        public const string c_Border_Top_Width = "border-top-width";
        public const string c_Border_Width = "border-width";
        public static Color DefaultBackColor = Color.White;
        public static Color DefaultColor = Color.Black;
        public const ButtonBorderStyle DefaultStyle = ButtonBorderStyle.Solid;
        public const int DefaultWidth = 0;
        public bool hasBackGround = false;
        public Color leftColor = DefaultColor;
        public ButtonBorderStyle leftStyle = ButtonBorderStyle.Solid;
        public int leftWidth = 0;
        public Color rightColor = DefaultColor;
        public ButtonBorderStyle rightStyle = ButtonBorderStyle.Solid;
        public int rightWidth = 0;
        public Color topColor = DefaultColor;
        public ButtonBorderStyle topStyle = ButtonBorderStyle.Solid;
        public int topWidth = 0;

        public ZYTextBorder()
        {
            this.Clear();
        }

        public void Clear()
        {
            this.leftWidth = 0;
            this.leftStyle = ButtonBorderStyle.Solid;
            this.topColor = DefaultColor;
            this.topWidth = 0;
            this.topStyle = ButtonBorderStyle.Solid;
            this.rightColor = DefaultColor;
            this.rightWidth = 0;
            this.rightStyle = ButtonBorderStyle.Solid;
            this.bottomColor = DefaultColor;
            this.bottomWidth = 0;
            this.bottomStyle = ButtonBorderStyle.Solid;
            this.hasBackGround = false;
            this.backColor = DefaultBackColor;
        }

        public ZYTextBorder Clone()
        {
            ZYTextBorder border = new ZYTextBorder();
            border.CopyFrom(this);
            return border;
        }

        public void CopyFrom(ZYTextBorder b)
        {
            if (b != null)
            {
                this.leftColor = b.leftColor;
                this.leftStyle = b.leftStyle;
                this.leftWidth = b.leftWidth;
                this.topColor = b.topColor;
                this.topStyle = b.topStyle;
                this.topWidth = b.topWidth;
                this.rightColor = b.rightColor;
                this.rightStyle = b.rightStyle;
                this.rightWidth = b.rightWidth;
                this.bottomColor = b.bottomColor;
                this.bottomStyle = b.bottomStyle;
                this.bottomWidth = b.bottomWidth;
            }
        }

        public void Draw(DocumentView view, Rectangle myRect)
        {
            view.DrawBorder(myRect, this.leftColor, this.leftWidth, this.leftStyle, this.topColor, this.topWidth, this.topStyle, this.rightColor, this.rightWidth, this.rightStyle, this.bottomColor, this.bottomWidth, this.bottomStyle);
        }

        public void Draw(DocumentView view, int left, int top, int width, int height)
        {
            this.Draw(view, new Rectangle(left, top, width, height));
        }

        public bool DrawBackGround(Graphics myGraph, Rectangle myRect)
        {
            if (((myGraph != null) && this.hasBackGround) && !myRect.IsEmpty)
            {
                using (SolidBrush brush = new SolidBrush(this.backColor))
                {
                    myGraph.FillRectangle(brush, myRect);
                }
                return true;
            }
            return false;
        }

        public bool EqualBorder(ZYTextBorder b)
        {
            return ((this == b) || ((b != null) && ((((((this.leftWidth == b.leftWidth) && (this.topWidth == b.topWidth)) && ((this.rightWidth == b.rightWidth) && (this.bottomWidth == b.bottomWidth))) && ((this.leftStyle.Equals(b.leftStyle) && this.topStyle.Equals(b.topStyle)) && (this.rightStyle.Equals(b.rightStyle) && this.bottomStyle.Equals(b.bottomStyle)))) && (((this.leftColor.Equals(b.leftColor) && this.topColor.Equals(b.topColor)) && (this.rightColor.Equals(b.rightColor) && this.bottomColor.Equals(b.bottomColor))) && (this.hasBackGround == b.hasBackGround))) && (this.backColor == b.backColor))));
        }

        public bool FromXML(XmlElement myElement)
        {
            this.Clear();
            return this.FromXMLWithoutClear(myElement);
        }

        public bool FromXMLWithoutClear(XmlElement myElement)
        {
            if (myElement != null)
            {
                if (myElement.HasAttribute("border-color"))
                {
                    this.BorderColor = StringCommon.ColorFromHtml(myElement.GetAttribute("border-color"), DefaultColor);
                }
                if (myElement.HasAttribute("border-width"))
                {
                    this.BorderWidth = StringCommon.ToInt32Value(myElement.GetAttribute("border-width"), 0);
                }
                if (myElement.HasAttribute("border-style"))
                {
                    this.BorderStyle = ToBorderStyle(myElement.GetAttribute("border-style"));
                }
                if (myElement.HasAttribute("border-left-color"))
                {
                    this.leftColor = StringCommon.ColorFromHtml(myElement.GetAttribute("border-left-color"), DefaultColor);
                }
                if (myElement.HasAttribute("border-left-width"))
                {
                    this.leftWidth = StringCommon.ToInt32Value(myElement.GetAttribute("border-left-width"), 0);
                }
                if (myElement.HasAttribute("border-left-style"))
                {
                    this.leftStyle = ToBorderStyle(myElement.GetAttribute("border-left-style"));
                }
                if (myElement.HasAttribute("border-top-color"))
                {
                    this.topColor = StringCommon.ColorFromHtml(myElement.GetAttribute("border-top-color"), DefaultColor);
                }
                if (myElement.HasAttribute("border-top-width"))
                {
                    this.topWidth = StringCommon.ToInt32Value(myElement.GetAttribute("border-top-width"), 0);
                }
                if (myElement.HasAttribute("border-top-style"))
                {
                    this.topStyle = ToBorderStyle(myElement.GetAttribute("border-top-style"));
                }
                if (myElement.HasAttribute("border-right-color"))
                {
                    this.rightColor = StringCommon.ColorFromHtml(myElement.GetAttribute("border-right-color"), DefaultColor);
                }
                if (myElement.HasAttribute("border-right-width"))
                {
                    this.rightWidth = StringCommon.ToInt32Value(myElement.GetAttribute("border-right-width"), 0);
                }
                if (myElement.HasAttribute("border-right-style"))
                {
                    this.rightStyle = ToBorderStyle(myElement.GetAttribute("border-right-style"));
                }
                if (myElement.HasAttribute("border-bottom-color"))
                {
                    this.bottomColor = StringCommon.ColorFromHtml(myElement.GetAttribute("border-bottom-color"), DefaultColor);
                }
                if (myElement.HasAttribute("border-bottom-width"))
                {
                    this.bottomWidth = StringCommon.ToInt32Value(myElement.GetAttribute("border-bottom-width"), 0);
                }
                if (myElement.HasAttribute("border-bottom-style"))
                {
                    this.bottomStyle = ToBorderStyle(myElement.GetAttribute("border-bottom-style"));
                }
                this.hasBackGround = myElement.HasAttribute("background-color");
                if (this.hasBackGround)
                {
                    this.backColor = StringCommon.ColorFromHtml(myElement.GetAttribute("background-color"), DefaultBackColor);
                }
                else
                {
                    this.backColor = DefaultBackColor;
                }
                return true;
            }
            return false;
        }

        public static bool TestXML(XmlElement myElement)
        {
            return (((((myElement != null) || myElement.HasAttribute("border-color")) || (myElement.HasAttribute("border-width") || myElement.HasAttribute("border-style"))) || ((myElement.HasAttribute("border-left-color") || myElement.HasAttribute("border-left-width")) || (myElement.HasAttribute("border-left-style") || myElement.HasAttribute("border-top-color")))) || (((myElement.HasAttribute("border-top-width") || myElement.HasAttribute("border-top-style")) || (myElement.HasAttribute("border-right-color") || myElement.HasAttribute("border-right-width"))) || ((myElement.HasAttribute("border-right-style") || myElement.HasAttribute("border-bottom-color")) || ((myElement.HasAttribute("border-bottom-width") || myElement.HasAttribute("border-bottom-style")) || myElement.HasAttribute("background-color")))));
        }

        private static ButtonBorderStyle ToBorderStyle(string strValue)
        {
            try
            {
                return (ButtonBorderStyle) Enum.Parse(ButtonBorderStyle.Solid.GetType(), strValue, true);
            }
            catch
            {
                return ButtonBorderStyle.Solid;
            }
        }

        public bool ToXML(XmlElement myElement)
        {
            if (myElement != null)
            {
                if ((this.leftColor.Equals(this.topColor) && this.topColor.Equals(this.rightColor)) && this.rightColor.Equals(this.bottomColor))
                {
                    if (!this.leftColor.Equals(DefaultColor))
                    {
                        myElement.SetAttribute("border-color", StringCommon.ColorToHtml(this.leftColor));
                    }
                }
                else
                {
                    if (!this.leftColor.Equals(DefaultColor))
                    {
                        myElement.SetAttribute("border-left-color", StringCommon.ColorToHtml(this.leftColor));
                    }
                    if (!this.topColor.Equals(DefaultColor))
                    {
                        myElement.SetAttribute("border-top-color", StringCommon.ColorToHtml(this.topColor));
                    }
                    if (!this.rightColor.Equals(DefaultColor))
                    {
                        myElement.SetAttribute("border-right-color", StringCommon.ColorToHtml(this.rightColor));
                    }
                    if (!this.bottomColor.Equals(DefaultColor))
                    {
                        myElement.SetAttribute("border-bottom-color", StringCommon.ColorToHtml(this.bottomColor));
                    }
                }
                if (((this.leftWidth == this.topWidth) && (this.topWidth == this.rightWidth)) && (this.rightWidth == this.bottomWidth))
                {
                    if (this.leftWidth != 0)
                    {
                        myElement.SetAttribute("border-width", this.leftWidth.ToString());
                    }
                }
                else
                {
                    if (this.leftWidth != 0)
                    {
                        myElement.SetAttribute("border-left-width", this.leftWidth.ToString());
                    }
                    if (this.topWidth != 0)
                    {
                        myElement.SetAttribute("border-top-width", this.topWidth.ToString());
                    }
                    if (this.rightWidth != 0)
                    {
                        myElement.SetAttribute("border-right-width", this.rightWidth.ToString());
                    }
                    if (this.bottomWidth != 0)
                    {
                        myElement.SetAttribute("border-bottom-width", this.bottomWidth.ToString());
                    }
                }
                if (((this.leftStyle == this.topStyle) && (this.topStyle == this.rightStyle)) && (this.rightStyle == this.bottomStyle))
                {
                    if (!this.leftStyle.Equals(ButtonBorderStyle.Solid))
                    {
                        myElement.SetAttribute("border-style", this.leftStyle.ToString().ToLower());
                    }
                }
                else
                {
                    if (this.leftStyle != ButtonBorderStyle.Solid)
                    {
                        myElement.SetAttribute("border-left-style", this.leftStyle.ToString().ToLower());
                    }
                    if (this.topStyle != ButtonBorderStyle.Solid)
                    {
                        myElement.SetAttribute("border-top-style", this.topStyle.ToString().ToLower());
                    }
                    if (this.rightStyle != ButtonBorderStyle.Solid)
                    {
                        myElement.SetAttribute("border-right-style", this.rightStyle.ToString().ToLower());
                    }
                    if (this.bottomStyle != ButtonBorderStyle.Solid)
                    {
                        myElement.SetAttribute("border-bottom-style", this.bottomStyle.ToString().ToLower());
                    }
                }
                if (this.hasBackGround)
                {
                    myElement.SetAttribute("background-color", StringCommon.ColorToHtml(this.backColor));
                }
                return true;
            }
            return false;
        }

        public bool ToXMLEx(XmlElement myElement, ZYTextBorder b)
        {
            if (((myElement != null) && (b != null)) && (b != this))
            {
                if ((this.leftColor.Equals(this.topColor) && this.topColor.Equals(this.rightColor)) && this.rightColor.Equals(this.bottomColor))
                {
                    if (!this.leftColor.Equals(DefaultColor) && !this.leftColor.Equals(b.leftColor))
                    {
                        myElement.SetAttribute("border-color", StringCommon.ColorToHtml(this.leftColor));
                    }
                }
                else
                {
                    if (!this.leftColor.Equals(DefaultColor) && !this.leftColor.Equals(b.leftColor))
                    {
                        myElement.SetAttribute("border-left-color", StringCommon.ColorToHtml(this.leftColor));
                    }
                    if (!this.topColor.Equals(DefaultColor) && !this.topColor.Equals(b.topColor))
                    {
                        myElement.SetAttribute("border-top-color", StringCommon.ColorToHtml(this.topColor));
                    }
                    if (!this.rightColor.Equals(DefaultColor) && !this.rightColor.Equals(b.rightColor))
                    {
                        myElement.SetAttribute("border-right-color", StringCommon.ColorToHtml(this.rightColor));
                    }
                    if (!this.bottomColor.Equals(DefaultColor) && !this.bottomColor.Equals(b.bottomColor))
                    {
                        myElement.SetAttribute("border-bottom-color", StringCommon.ColorToHtml(this.bottomColor));
                    }
                }
                if (((this.leftWidth == this.topWidth) && (this.topWidth == this.rightWidth)) && (this.rightWidth == this.bottomWidth))
                {
                    if ((this.leftWidth != 0) && (this.leftWidth != b.leftWidth))
                    {
                        myElement.SetAttribute("border-width", this.leftWidth.ToString());
                    }
                }
                else
                {
                    if ((this.leftWidth != 0) && (this.leftWidth != b.leftWidth))
                    {
                        myElement.SetAttribute("border-left-width", this.leftWidth.ToString());
                    }
                    if ((this.topWidth != 0) && (this.topWidth != b.topWidth))
                    {
                        myElement.SetAttribute("border-top-width", this.topWidth.ToString());
                    }
                    if ((this.rightWidth != 0) && (this.rightWidth != b.rightWidth))
                    {
                        myElement.SetAttribute("border-right-width", this.rightWidth.ToString());
                    }
                    if ((this.bottomWidth != 0) && (this.bottomWidth != b.bottomWidth))
                    {
                        myElement.SetAttribute("border-bottom-width", this.bottomWidth.ToString());
                    }
                }
                if (((this.leftStyle == this.topStyle) && (this.topStyle == this.rightStyle)) && (this.rightStyle == this.bottomStyle))
                {
                    if (!this.leftStyle.Equals(ButtonBorderStyle.Solid) && !this.leftStyle.Equals(b.leftStyle))
                    {
                        myElement.SetAttribute("border-style", this.leftStyle.ToString().ToLower());
                    }
                }
                else
                {
                    if ((this.leftStyle != ButtonBorderStyle.Solid) && (this.leftStyle != b.leftStyle))
                    {
                        myElement.SetAttribute("border-left-style", this.leftStyle.ToString().ToLower());
                    }
                    if ((this.topStyle != ButtonBorderStyle.Solid) && (this.topStyle != b.topStyle))
                    {
                        myElement.SetAttribute("border-top-style", this.topStyle.ToString().ToLower());
                    }
                    if ((this.rightStyle != ButtonBorderStyle.Solid) && (this.rightStyle != b.rightStyle))
                    {
                        myElement.SetAttribute("border-right-style", this.rightStyle.ToString().ToLower());
                    }
                    if ((this.bottomStyle != ButtonBorderStyle.Solid) && (this.bottomStyle != b.bottomStyle))
                    {
                        myElement.SetAttribute("border-bottom-style", this.bottomStyle.ToString().ToLower());
                    }
                }
                if ((this.hasBackGround && (this.hasBackGround != b.hasBackGround)) && (this.backColor != b.backColor))
                {
                    myElement.SetAttribute("background-color", StringCommon.ColorToHtml(this.backColor));
                }
                return true;
            }
            return false;
        }

        public Color BorderColor
        {
            get
            {
                return this.leftColor;
            }
            set
            {
                this.leftColor = value;
                this.topColor = value;
                this.rightColor = value;
                this.bottomColor = value;
            }
        }

        public ButtonBorderStyle BorderStyle
        {
            get
            {
                return this.leftStyle;
            }
            set
            {
                this.leftStyle = value;
                this.topStyle = value;
                this.rightStyle = value;
                this.bottomStyle = value;
            }
        }

        public int BorderWidth
        {
            get
            {
                return this.leftWidth;
            }
            set
            {
                this.leftWidth = value;
                this.topWidth = value;
                this.rightWidth = value;
                this.bottomWidth = value;
            }
        }
    }
}

